﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
namespace WPFExcelReport.ViweModels
{
    //TBList="HRSTAMM;ICC_FOLDERS;ICC_FUNCTIONS;ICC_MENUDEF;ICC_MULLANG;ICC_GABBS;ICC_WFEVENTDEF;ICC_WFEVENTSCLASS;ICC_WFEVENTSGROUP;ICC_WFSUBSCRIBER;";//ITXSETUP
    public class MainViewModel : BaseFunction
    {
        
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public Dictionary<string, object> dicParamater
        {
            get;
            set;
        }
        public string Mandant
        {
            get
            {
                return (string)dicParamater["MANDANT"];
            }
        }
        public string company
        {
            get
            {
                return (string)dicParamater["Company"];
            }
        }
        
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public SETINTEXDB SetEnv
        {
            get;
            set;

        }
        public SETINTEXDB SetDetail
        {
            get;
            set;
        }
        public SETINTEXDB SetMaster
        {
            get;
            set;
        }
        public MainViewModel()
        {
            if (SetEnv == null)
            {
                SetEnv = new SETINTEXDB();
            }
            if (SetMaster == null)
            {
                SetMaster = new SETINTEXDB();
            }
            if (SetDetail == null)
            {
                SetDetail = new SETINTEXDB();
            }
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
        } 
        public DataSet LoadEnv(string client)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                if (INTEXDBC == null)
                {
                    return setReturn;
                }
                DataRow dwData = SetEnv.HRSTAMM.NewRow();
                dwData["HRMANDANT"] = client;
                setResult = INTEXDBC.QueryHRSTAMM(dwData, 4);
                setReturn.Merge(setResult);
                dwData = SetEnv.ICC_FUNCTIONS.NewRow();
                dwData["MANDANT"] = client;
                setResult = INTEXDBC.QueryICC_FUNCTIONS(dwData, 9999);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet ReSetMaster()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet settemp = new DataSet();

            ArrayList tables = new ArrayList();
            string fileroot = Path.Combine(PathHelper.ApplicationPath, @"Documents");
            string filePath = "";

            string[] files = System.IO.Directory.GetFiles(fileroot);
            //string filepath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\ITXSETUP.xls");
            string ShortName = "";
            try
            {
                foreach (string file in files)
                {
                    if (!file.EndsWith("SETUP.xls"))
                        continue;
                    if (exAgent == null)
                    {
                        exAgent = new ExcelX.ExcelLib.ExcelAgent(file);
                    }
                    setResult = exAgent.LoadDataTableDef(file, "");

                    foreach (DataTable tbTemp in setResult.Tables)
                    {
                        string tbName = tbTemp.TableName;
                        settemp = new DataSet();
                        settemp.DataSetName = "SETUP";
                        settemp.Merge(tbTemp);
                        filePath = fileroot + "\\" + tbName + ".XML";
                        if (System.IO.File.Exists(filePath))
                        {
                            System.IO.File.Delete(filePath);
                        }
                        settemp.WriteXml(filePath);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                exAgent.KillExcelProcess();
                exAgent.Close();
            }
            return setReturn;
        }

    }
}
